<?php defined('SYSPATH') or die('No direct script access.');
/**
 * SwfUpload 文件上传
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: swfupload.php 1 2014-01-13 15:05:51Z zhouyl $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
class Controller_Admin_Swfupload extends Kohana_Controller
{
    /**
     * 控制器初始化
     */
    public function before()
    {
        $session_id = Arr::get($this->request->post(), 'session_id');
        if ($session_id)
        {
            Session::instance()->read($session_id);
        }

        parent::before();
    }

    /**
     * 常规上传
     */
    public function action_general()
    {
        $config = array(
            'exts'   => array('jpg', 'bmp', 'gif', 'png'),
            'size'   => 4096, // 4 MB
            'path'   => 'general',
            'rename' => 'md5'
        );

        $file_data = (array) Arr::get($_FILES, 'swfupload');

        $data = $this->_upload($config, $file_data);
        exit(JSON::encode($data, TRUE));
    }

    /**
     * 商品图片上传
     */
    public function action_item()
    {
        $config = array(
            'exts'   => array('jpg', 'bmp', 'gif', 'png'),
            'size'   => 2048, // 4 MB
            'path'   => 'item',
            'rename' => 'md5'
        );

        $file_data = (array) Arr::get($_FILES, 'swfupload');

        $data = $this->_upload($config, $file_data);
        exit(JSON::encode($data, TRUE));
    }

    /**
     * swfupload 文件统一上传处理
     *
     * @param  array   $config
     * @param  array   $file_data
     * @return array
     */
    protected function _upload(array $config, array $file_data)
    {
        try
        {
            if ( ! Manager::instance()->is_logined())
            {
                throw new Exception('权限不足');
            }

            $config['path'] = UPDPATH.'/'.$config['path'];

            $upload = Upload::factory($config);
            $result = $upload->save($file_data);

            $data = array(
                'success' => TRUE,
                'file'    => $result['file'],
                'name'    => $result['name'],
                'base'    => basename($result['file']),
                'size'    => $result['size'],
                'type'    => $result['type'],
                'ext'     => $result['ext'],
            );

            if ($config['rename'] == 'md5')
            {
                $prefix = str_ireplace(UPDPATH, '', $config['path']);
                if ($upload->is_image($data['file']))
                {
                    $data['thumb'] = URL::image($data['base'], '100',  $prefix);
                    $data['cover'] = URL::image($data['base'], '200',  $prefix);
                    $data['url']   = URL::image($data['base'], NULL, $prefix);
                }
            }
        }
        catch (Exception $e)
        {
            $data = array(
                'success'  => FALSE,
                'message'  => $e->getMessage(),
            );
        }

        return $data;
    }

    /**
     * 防止 session_id 丢失
     */
    public function action_session()
    {
        exit();
    }

    /**
     * 上传测试
     */
    public function action_test()
    {
        $view = new View;
        $this->response->body($view->render('admin/swfupload'));
    }

}
